package com.projrct;

import javax.swing.*;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Properties;

public class ATM {
    public Properties pro = new Properties();


    public ATM() {
        try {
            pro.load(new FileReader("Class8_24/src/ATM.txt"));
        }catch (Exception e){
            System.out.println("文件不存在");
        }
    }


    //登录
    public void login(){
        for (int i=0;i<3;i++){
            String inputCount = JOptionPane.showInputDialog(null,"请输入用户名");
            String inputPwd = JOptionPane.showInputDialog(null,"请输入密码");
            if (pro.getProperty("userName").equals(inputCount)&&pro.getProperty("pwd").equals(inputPwd)){
                JOptionPane.showMessageDialog(null,"登录成功");
                break;
            }else {
                JOptionPane.showMessageDialog(null,"账号密码有误，重新登录");
            }
            if (i==2){
                System.out.println("非法输入");
                System.exit(0);
            }
        }
    }


    //信息查询
    public void getInform(){
        JOptionPane.showMessageDialog(null,"当前余额未："+pro.getProperty("money"));
    }

    //存钱
    public void save(){
        int num = Integer.parseInt(JOptionPane.showInputDialog(null,"输入存款金额"));
        String a = pro.getProperty("money");
        int now = Integer.parseInt(a)+num;
        pro.setProperty("money",now+"");
        this.fileSave();
        this.getInform();
    }

    //取钱
    public void getMoney(){
        int number = Integer.parseInt(JOptionPane.showInputDialog(null,"输入取款金额"));
        int money =Integer.parseInt(pro.getProperty("money"));
        if (number > money){
            JOptionPane.showMessageDialog(null,"余额不足");
            return;
        }
        pro.setProperty("money",money-number+"");
        this.fileSave();
        this.getInform();
    }

    //密码修改
    public void pwdChange(){
        String oldPwd = JOptionPane.showInputDialog(null,"输入原密码");
        if (oldPwd.equals(pro.getProperty("pwd"))){

            String newPwd = JOptionPane.showInputDialog(null,"输入新密码");
            String rePwd = JOptionPane.showInputDialog(null,"确认新密码");
            if (!newPwd.equals(rePwd)){
                JOptionPane.showMessageDialog(null,"确认密码不相同");
                return;
            }

            pro.setProperty("pwd",newPwd);
            this.fileSave();
        }
    }

    //文件重储
    public void fileSave(){
        try {
            pro.store(new FileWriter("Class8_24/src/ATM.txt"),null);
        }catch (Exception e){
            System.out.println("文件不存在");
        }
    }












}
